/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package nl.windesheim.archdemo.presentationmodel;

import java.util.ArrayList;
import java.util.List;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;
import nl.windesheim.archdemo.dao.CustomerDao;
import nl.windesheim.archdemo.dao.GlobalInstances;
import nl.windesheim.archdemo.model.Address;
import nl.windesheim.archdemo.model.Customer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;

public class CustomerTablePresenter implements TableModel {

    private CustomerDao customerDao = GlobalInstances.getCustomerDao();

    /* use only through getter because of lazy loading! */
    private List<Customer> customers;
    private List<TableModelListener> listeners;

    /**
     * Lazy loading of order list
     * @return 
     */
    private List<Customer> getCustomers() {
        if (this.customers == null) {
            customers = customerDao.getAll();
        }
        return customers;
    }

    @Override
    public int getRowCount() {
        return this.getCustomers().size();
    }

    @Override
    public int getColumnCount() {
        return 7;
    }

    @Override
    public String getColumnName(int i) {
        switch (i) {
            case 0:
                return "id";
            case 1:
                return "Naam";
            case 2:
                return "Straat";
            case 3:
                return "Nummer";
            case 4:
                return "Postcode";
            case 5:
                return "Plaats";
            case 6:
                return "Land";
            default:
                throw new ArrayIndexOutOfBoundsException("Customer table has only 7 cols");
        }
    }

    @Override
    public Class<?> getColumnClass(int i) {
        return String.class;
    }

    @Override
    public boolean isCellEditable(int i, int i1) {
        return false;
    }

    @Override
    public Object getValueAt(int row, int col) {
        Customer c = getRowModel(row);
        Object value;
        switch (col) {
            case 0:
                value = c.getId();
                break;
            case 1:
                value = c.getName();
                break;
            case 2:
                value = c.getAddress().getStreetName();
                break;
            case 3:
                Address a = c.getAddress();
                value = String.format("%s %s", a.getHouseNumber(), a.getNumberExtension());
                break;
            case 4:
                value = c.getAddress().getPostcode();
                break;
            case 5:
                value = c.getAddress().getCity();
                break;
            case 6:
                value = c.getAddress().getCountry();
                break;
            default:
                throw new ArrayIndexOutOfBoundsException("Customer table has only 7 cols");
        }
        return value.toString();
    }

    @Override
    public void setValueAt(Object o, int i, int i1) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void addTableModelListener(TableModelListener tl) {
        if (listeners == null) {
            listeners = new ArrayList<TableModelListener>(1);
            listeners.add(tl);
        }
    }

    @Override
    public void removeTableModelListener(TableModelListener tl) {
        if (listeners != null) {
            listeners.remove(tl);
        }
    }

    public Customer getRowModel(int row) {
        return this.getCustomers().get(row);
    }
}
